<template>
	<view class="c-block-full z-box">

		<view class="c-block z-item1" >
			<view class="z-item" style="display: flex;" :style="paper.themequestionfont+';'+paper.themequestiondivstyle">
        <view class="block-inline a-required" :style="paper.themequestiondivstyle" v-if="field.csmwjrequired.value===true">*</view>
				<view v-if="paper.themequestionnoshow=='Y'" class="c-content block-inline">{{index}}.</view>
				<!-- <view class="c-content block-inline"><u-parse :content="field.csmwjtitle.value" ></u-parse></view> -->
        <pre class="c-content block-inline zxb-pre" >{{field.csmwjtitle.value}}</pre>
        
			</view>
			<view class="wrap" >
				<u-row gutter="16">
					<u-col span="4" v-for="(item,index) in field.csmwjimages.value" :key="index">
						<u-image width="100%" :src="item.url" mode="widthFix"></u-image>
					</u-col>
				</u-row>
			</view>
			 <!-- disable="csmwj-input-fixed-score" -->
			<u-input  v-model="val" :disabled="field.__config__.tag=='csmwj-input-fixed-score'" :type="field.csmwjinput.format"  v-if="field.csmwjinput.inputtype=='input'"
			   @input="change" />
			<u-input  v-model="val" type="textarea"  v-if="field.csmwjinput.inputtype=='textarea'"
			  @input="change" />
			<view class="c-errormsg" v-if="errormsg!=null">{{errormsg}}</view>
		</view>

	</view>
</template>

<script>
	export default {
		name: 'c-parsefg-csmwj-input',
		props: {
			field: {
				type: Object
			},
			answer: {
				type: Object
			},
			paper: {
				type: Object
			},
			index: {
				type: Number
			},
		},
		data() {
			return {
				val: null,
				errormsg: null,
				inputClass: "c-normalinput",
			};
		},
		mounted() {
			let that = this;
			that.cinit();
      // console.log('field---------', this.field)
		},
		methods: {
			/**
			 * spa. 将__answer__ 初始化到页面显示(用于未完成续答)
			 */
			cinit:function(){
				let that = this;
				if(that.field.__answer__.value==null || that.field.__answer__.value=='' ){
					that.val = that.field.csmwjinput.value;
				}else{
					that.val = that.field.__answer__.value;
				}
				
			},
			/**
			 * spa. 校验格式
			 * spb. 校验必填项
			 */
			__submit:function(){
				let that = this;
				let field = that.field;
				//spa
				let ret = that.change(that.val);
				if(ret===false){
					return false;
				}
				//spb
				if(field.csmwjrequired.value===true && (that.val==null || that.val=="")){
					that._setErrorMsg(that.$t('csmwj.validate.required'));
					return false;
				}
				that._setOkMsg();
        
				//return {value:that.val};
				return that.__getVal();
			},
			__getVal:function(){
				let that = this;
				return {value:that.val};
			},
			change: function(val) {
				// console.log('val==',val); 
				let that = this;
				let csmwjinput = that.field.csmwjinput;
				switch (csmwjinput.format) {
					case "text":
						let ll = (val == null) ? 0 : val.length;
						if (ll < csmwjinput.format_text_minlength || ll > csmwjinput.format_text_maxlength) {
							//that._setErrorMsg('文字长度不能在' + csmwjinput.format_text_minlength + '到' + csmwjinput.format_text_maxlength + '之间');
							that._setErrorMsg(that.$t('csmwj.validate.string_length_range',{min:csmwjinput.format_text_minlength,max:csmwjinput.format_text_maxlength}));
							return false;
						} else {
							that._setOkMsg();
						}
						break;
					case "number":
						let valFlout = parseFloat(val);
						let val2 = valFlout.toFixed(csmwjinput.format_number_decimal_length);
            this.$emit('res',val)
						if (val == valFlout && valFlout == val2) {
							if (val2 < csmwjinput.format_number_min || val2 > csmwjinput.format_number_max) {
								//that._setErrorMsg('输入数字在' + csmwjinput.format_number_min + '到' + csmwjinput.format_number_max + '之间');						
								that._setErrorMsg(that.$t('csmwj.validate.int_range',{min:csmwjinput.format_number_min,max:csmwjinput.format_number_max}));							
								return false;
							} else {
								that._setOkMsg();
							}
						} else {
							//that._setErrorMsg('输入数字不能超过' + csmwjinput.format_number_decimal_length + '位小数');
							that._setErrorMsg(that.$t('csmwj.validate.float_decimal_length',{length:csmwjinput.format_number_decimal_length}));	
							return false;
						}
						break;
					case "email":
						let reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
						if (reg.test(val)) {
							that._setOkMsg();	
						} else {
							//that._setErrorMsg('请输入邮箱地址');	
							that._setErrorMsg(that.$t('csmwj.validate.email'));	
							return false;					
						}
						break;
					case "idcard":
						if (that._checkIDCard(val)) {
							that._setOkMsg();	
						} else {
							//that._setErrorMsg('请输入身份证号码');	
							that._setErrorMsg(that.$t('csmwj.validate.idcard'));
							return false;
						}
						break;
					case "regex":
						let reg2 = new RegExp(csmwjinput.format_regex);; 
						console.log('----',val,reg2);
						console.log('==',reg2.test(val));
						// if (val.match(reg2)) {
						if (reg2.test(val)) {
							that._setOkMsg();	
						} else {
							that._setErrorMsg(csmwjinput.format_regex_errormsg);	
							return false;
						}
						break;
				} 
				return true;
			},
			_setErrorMsg: function(msg) {
				let that = this;
				that.errormsg = msg;
				that.inputClass = "c-errorinput";
			},
			_setOkMsg: function() {
				let that = this;
				that.errormsg = '';
				that.inputClass = "c-normalinput";
			},
			_checkIDCard:function(idcode){
			    // 加权因子
			    var weight_factor = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
			    // 校验码
			    var check_code = ['1', '0', 'X' , '9', '8', '7', '6', '5', '4', '3', '2'];
			
			    var code = idcode + "";
			    var last = idcode[17];//最后一位
			
			    var seventeen = code.substring(0,17);
			
			    // ISO 7064:1983.MOD 11-2
			    // 判断最后一位校验码是否正确
			    var arr = seventeen.split("");
			    var len = arr.length;
			    var num = 0;
			    for(var i = 0; i < len; i++){
			        num = num + arr[i] * weight_factor[i];
			    }
			    
			    // 获取余数
			    var resisue = num%11;
			    var last_no = check_code[resisue];
			
			    // 格式的正则
			    // 正则思路
			    /*
			    第一位不可能是0
			    第二位到第六位可以是0-9
			    第七位到第十位是年份，所以七八位为19或者20
			    十一位和十二位是月份，这两位是01-12之间的数值
			    十三位和十四位是日期，是从01-31之间的数值
			    十五，十六，十七都是数字0-9
			    十八位可能是数字0-9，也可能是X
			    */
			    var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
			
			    // 判断格式是否正确
			    var format = idcard_patter.test(idcode);
			
			    // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
			    return last === last_no && format ? true : false;
			},
		}
	};
</script>

<style scoped lang="scss">
	.a-required {
		padding-right: 6rpx !important; background-color: transparent !important;
		color: red; 
		font-weight: bold;
	}

	.a-score {
		padding-left: 10rpx;
	}

	.u-radio-group {
		width: 100%;
	}
	.u-input{
		margin-top:10px;
	}
 .c-block{padding: 0;}
</style>
